<template>
  <!-- 呆滞件交易查询 -->
  <div>
    <TopFilter :list="topFilterList" :cols="3" :rows="2" :labelWidth="120" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partExchangeQueryIndex"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="publishInfo">信息发布</el-button> -->
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" width="600px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <Delivery :partPlatformOrderId="partPlatformOrderId" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { queryShortPart } from '@/api/part/exchange/platform';
import { getOrderInfo, confirmOrder, shippedOrder, closeOrder } from '@/api/part/exchange/query';
import Delivery from './delivery';
import { dictionary } from '@/utils/dictMixin';
import { notifyAction } from '@/utils';
import { getAllUser } from '@/api/part/common';
export default {
  name: 'SparePartsQuery',
  components: { Delivery },
  mixins: [dictionary],
  data() {
    this.pruchaseInfos = { price: '销售', number: '剩余', details: '购买' };
    return {
      topFilterList: this.createTopFilters(),
      columns: this.createTableColumns(),
      // 开启ajax请求
      fetchParams: { xhrAbort: false },
      // 条件查询接口
      fetchApi: getOrderInfo,
      visible: false,
      dialogTitle: '',
      titleName: '',
      type: '',
      partPlatformOrderId: {},
      userList: [] // 人员列表
    };
  },
  created() {
    this.getUserList();
  },

  methods: {
    // 获取所有员工
    async getUserList() {
      let res = await getAllUser();
      if (res.resultCode == 200) {
        res.data.map(e => {
          this.userList[e.userId] = e.employeeName;
        });
      } else [this.$notify.error({ title: '提示信息', message: '获取人员列表失败' })];
    },
    // 关闭
    async closeHandle(val) {
      const res = await this.$prompt('关闭理由', '关闭订单', {
        confirmButtonText: '确定关闭',
        cancelButtonText: '取消',
        // required: true
        inputValidator: val => {
          if (val === null) {
            return false;
          }
        },
        inputErrorMessage: '请输入关闭理由'
      }).catch(() => {});
      if (res != null) {
        val.closeCause = res.value;
        console.log(val);

        let result = await closeOrder(val);
        if (result.resultCode == 200) {
          this.fetchParams = { ...this.fetchParams };
          notifyAction('关闭成功');
        }
        // 调用后台处理接口
        // let res = await
      }
    },

    // 确定
    async confirmHandle(val) {
      // console.log(val.saleType == 97011002);
      // console.log(val.saleType == 97011002 ? false : val.status == 97021001 ? true : false);

      await this.$confirm('是否确认订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.confirmOrder1(val);
        })
        .catch(() => {});
    },
    async confirmOrder1(val) {
      // console.log('12312', val);
      let res = await confirmOrder(val);
      // console.log(res, 123);

      if (res.resultCode == 200) {
        this.fetchParams = { ...this.fetchParams };
        let msg = '';
        if (res.data) {
          let arr = res.data.split(';');
          msg = arr[1];
        }
        this.$alert('请确认收到货款后,出库及发货', '恭喜您，成功的完成了一笔交易!销售单号为' + msg, {
          confirmButtonText: '关闭'
        });
      } else {
        this.$notity.error({ title: '提示信息', message: res.message });
      }
    },
    // 类型变化回调
    purchase(val) {
      alert(skkk);
    },
    // 替换列
    replaceColumns() {
      console.log('321');
    },
    async deliveryHandle(val) {
      this.partPlatformOrderId = val.partPlatformOrderId;
      this.visible = true;
      this.dialogTitle = '订单发货';
    },
    // 点击关闭按钮触发事件
    closeHandler(val, type, isSuccess) {
      this.visible = val;
      if (isSuccess) {
        this.$notify.success({ title: '提示信息', message: '发货成功' });
      }
      // 重新加载
      this.fetchParams = { ...this.fetchParams };
    },

    // 点击信息发布
    publishInfo() {
      // this.handleType = 'add';
      this.titleName = '信息发布';
      this.showHandler();
      this.visible = true;
    },
    // 删除提示
    async removeHandler(val) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      });
      await this.deleteDialog();
    },
    // 删除调用接口
    deleteDialog() {
      alert('正在删除');
    },
    // 显示处理
    showHandler() {
      this.dialogTitle = this.titleName;
    },

    // 点击搜索按钮触发的事件，参数是搜索条件对象
    changeHandle(val) {
      if (val.dEnd) {
        let arr = val.dEnd.split(' ');
        val.dEnd = arr[0].concat(' 23:59:59');
      }
      this.fetchParams = Object.assign({}, val);
    },

    // 展开-收起时的回调函数
    collapseHandle() {
      this.$nextTick(() => {
        this.$refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    // 校验是否确认
    checkConfirm(val) {
      // console.log('当前行数据', val);
      // 如果是购买->买家
      if (val.saleType == 97011002) {
        return false;
      }
      // 卖家未确认
      if (val.status != 97021001) {
        return false;
      }
      return true;
    },

    // 搜索条件
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          placeholder: ''
          // readonly: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName',
          placeholder: ''
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'status',
          initialValue: '',
          itemList: this.createDictList('9702'),
          change: this.purchase,
          placeholder: ''
        },
        {
          type: 'SELECT',
          label: '交易类别',
          fieldName: 'saleType',
          itemList: this.createDictList('9701'),
          placeholder: ''
        },
        {
          type: 'RANGE_DATE',
          label: '订单生成日期',
          style: { minWidth: '260px' },
          fieldName: 'dBegin|dEnd'
        }
      ];
    },
    // table列
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          width: 150,
          // sorter: true,
          disabled: true,
          render: props => {
            return (
              <div>
                <el-button disabled={props.row.saleType == 97011002 ? true : props.row.status == 97021001 ? false : true} size="mini" type="text" onClick={() => this.confirmHandle(props.row)}>
                  确认
                </el-button>
                <el-button
                  // 当订单状态=卖方未确认，买卖双方可以点击【关闭】，进行取消交易。
                  disabled={props.row.status == 97021001 ? false : props.row.saleType == 97011002 ? true : false}
                  disabled={props.row.status == 97021001 ? false : props.row.status == 97021005 ? true : props.row.saleType == 97011002 ? true : false}
                  size="mini"
                  type="text"
                  onClick={() => this.closeHandle(props.row)}
                >
                  关闭
                </el-button>
                <el-button
                  // 买家不能发货, 卖家发货->订单状态必须是已确认
                  disabled={props.row.saleType != 97011001 ? true : props.row.status == 97021002 ? false : true}
                  size="mini"
                  type="text"
                  onClick={() => this.deliveryHandle(props.row)}
                >
                  发货
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 100,
          // fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '交易单号',
          dataIndex: 'partPlatformOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '交易类别',
          dataIndex: 'saleType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('9701'),
          render: props => {
            return <span>{this.createDictText(props.row.saleType, '9701')}</span>;
          }
        },
        {
          title: '订单状态',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('9702'),
          render: props => {
            return <span>{this.createDictText(props.row.status, '9702')}</span>;
          }
        },
        {
          title: '单价',
          dataIndex: 'inOutTaxedPrice',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '合计金额',
          dataIndex: 'totalPrice',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        },
        {
          title: '快递方式',
          dataIndex: 'expressType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8114'),
          render: props => {
            return <span>{this.createDictText(props.row.expressType, '8114')}</span>;
          },
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '销售经销商代码',
          dataIndex: 'sOwnerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          // width: 140,
          showOverflowTooltip: true
        },
        {
          title: '销售经销商名称',
          dataIndex: 'sOwnerName',
          sorter: true,
          filter: true,
          filterType: 'input',
          // width: 140,
          showOverflowTooltip: true
        },
        {
          title: '销售联系人',
          dataIndex: 'sContacts',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售联系方式',
          dataIndex: 'sTel',
          sorter: true,
          filter: true,
          filterType: 'input',
          // width: 140,
          showOverflowTooltip: true
        },
        {
          title: '购买经销商代码',
          dataIndex: 'bOwnerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          // width: 140,
          showOverflowTooltip: true
        },
        {
          title: '购买经销商名称',
          dataIndex: 'bOwnerName',
          sorter: true,
          filter: true,
          filterType: 'input',
          // width: 140,
          showOverflowTooltip: true
        },
        {
          title: '购买联系人',
          dataIndex: 'bContacts',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '购买联系方式',
          dataIndex: 'bTel',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '购买收货地址',
          dataIndex: 'bAddr',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '订单生成日期',
          dataIndex: 'orderDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '物流名称',
          dataIndex: 'logistics',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '物流单号',
          dataIndex: 'deliveryOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '卖方确认人',
          dataIndex: 'confirmUserName',
          sorter: true,
          filter: true,
          filterType: 'input'
          // render: props => {
          //   return <span>{this.userList[props.row.confirmUser]}</span>;
          // }
        },
        {
          title: '卖方确认时间',
          dataIndex: 'confirmDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发货操作人',
          dataIndex: 'sendUserName',
          sorter: true,
          filter: true,
          filterType: 'input'
          // render: props => {
          //   return <span>{this.userList[props.row.sendUser]}</span>;
          // }
        },
        {
          title: '发货日期',
          dataIndex: 'sendDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '关闭理由',
          dataIndex: 'closeCause',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 140,
          showOverflowTooltip: true
        },
        {
          title: '关闭时间',
          dataIndex: 'closeDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '关闭操作人',
          dataIndex: 'closeUserName',
          sorter: true,
          filter: true,
          filterType: 'input'
          // render: props => {
          //   return <span>{this.userList[props.row.closeUser]}</span>;
          // }
        },
        {
          title: '库龄（天）',
          dataIndex: 'stockAge',
          sorter: true,
          filter: true,
          filterType: 'number'
        }
      ];
    }
  }
};
</script>

<style></style>
